﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;

using NUnit.Framework;
using EasyNet.Solr;
using EasyNet.Solr.Commons;
using EasyNet.Solr.Impl;
using EasyNet.Solr.Commons.Params;

namespace SolrExample.Tests.EasyNetSolr
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestFixture]
    public class SolrExample_Tests
    {
        private static readonly ISolrResponseParser<NamedList, ResponseHeader> binaryResponseHeaderParser = new BinaryResponseHeaderParser();
        private static readonly ISolrResponseParser<NamedList, QueryResults<SolrExampleResult>> binaryQueryResultsParser = new BinaryQueryResultsParser<SolrExampleResult>(new SolrExampleDeSerializer());
        private static readonly ISolrResponseParser<NamedList, IDictionary<string, IDictionary<string, IList<string>>>> binaryHighlightingParser = new BinaryHighlightingParser();
        private IDictionary<string, IDictionary<string, IList<string>>> hightlightResult;
        private static readonly ISolrResponseParser<NamedList, IDictionary<string, IList<FacetField>>> binaryFacetFieldsParser = new BinaryFacetFieldsParser();
        private IDictionary<string, IList<FacetField>> facetResult;

        [TestFixtureSetUp]
        public void InitializeAllTests()
        {
        }

        [Test]
        public void Test1()
        {
            string solrCoreUrls = "http://127.0.0.1:8080/solr-Example";
            string collectionName = "collection1";
            IList<SolrExample> solrExampleRecords = getSolrExampleRecords();
            List<string> engineUrls = solrCoreUrls.Split(';').ToList();

            if (solrExampleRecords != null && solrExampleRecords.Count > 0)
            {
                IObjectSerializer<SolrExample> objectSerializer = new SolrExampleSerializer();
                ISolrEngineCommand solrEngineCommand = new EasyNetEngineCommand();

                IDictionary<string, bool> postedResult = solrEngineCommand.Post(engineUrls, collectionName, objectSerializer.Serialize(solrExampleRecords));
                bool success = !(postedResult.Count == 0 || postedResult.Values.Contains(false));

                Console.WriteLine("Post result: " + success);
            }
        }

        [Test]
        public void Test2()
        {
            string solrCoreUrl = "http://127.0.0.1:8080/solr-Example/collection1";
            string searchQuery = "name:name1 OR name:name2";
            SolrQuery solrQuery = new SolrQuery(searchQuery);

            ISolrEngineCommand solrEngineCommand = new EasyNetEngineCommand();
            NamedList searchResult = solrEngineCommand.Query(solrCoreUrl, solrQuery);

            if (null != searchResult)
            {
                //解析返回头信息
                var responseHeader = binaryResponseHeaderParser.Parse(searchResult);
                Console.WriteLine("Query Status:{0} QTime:{1}", responseHeader.Status, responseHeader.QTime);


                //解析查询结果            
                QueryResults<SolrExampleResult> queryResult = binaryQueryResultsParser.Parse(searchResult);
                Console.WriteLine("Total:{0} Result:{1}", queryResult.NumFound, queryResult.Count);
                foreach (SolrExampleResult record in queryResult)
                {
                    Console.WriteLine("Time:{0} title:{1}", record.last_modified.ToString(), record.title[0].ToString());
                }


                //解析高亮
                hightlightResult = binaryHighlightingParser.Parse(searchResult);

                facetResult = binaryFacetFieldsParser.Parse(searchResult);
            }
        }

        [TestFixtureTearDown]
        public void CleanupAllTests() { }

        public IList<SolrExample> getSolrExampleRecords()
        {
            IList<SolrExample> listExample = new List<SolrExample>();
            for (var i = 1; i <= 6; i++)
            {
                SolrExample example = new SolrExample() {
                    id = (1000 + i).ToString(),
                    name = "name" + i,

                    weight = 4.5f * i,
                    inStock = (i % 2 == 0),

                    title = new List<string> { "title" + i, "ti tle" + i },
                    author = "author" + i,
                    description = "desc" + i,
                    keywords = "keywords" + i,
                    content = "中国人民content" + i,

                    d_d = 10000.12345 * i
                };
                if (i != 1)
                    example.last_modified = DateTime.Now;
                listExample.Add(example);
            }
            return listExample;
        }
    }

}
